package com.ycode.bang.anbang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.adapter.ImageAdapter;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.customview.MyGridView;
import com.ycode.bang.anbang.model.OrderDetil;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 新接收的工单详情
 */
public class RepairDetailActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.tvt_address)
    TextView mTvAddress;
    @InjectView(R.id.tvt_name)
    TextView mTvtName;
    @InjectView(R.id.tvt_number)
    TextView mTvNumber;
    @InjectView(R.id.tvt_leixing)
    TextView mTvLeixing;
    @InjectView(R.id.tvt_neirong)
    TextView mTvNeirong;
    @InjectView(R.id.tvt_jibie)
    TextView mTvJibie;
    @InjectView(R.id.tvt_shijian)
    TextView mTvShijian;
    @InjectView(R.id.tvt_laiyuan)
    TextView mTvLaiyuan;
    @InjectView(R.id.gridview)
    MyGridView mGridView;
    @InjectView(R.id.btn_distribution)
    Button mBtnDistra;//分配按钮
    @InjectView(R.id.btn_deal)
    Button mBtnDeal;//直接处理按钮
    @InjectView(R.id.lin_top)
    LinearLayout linTop;

    @InjectView(R.id.lin_guanli)
    LinearLayout linGuanli;//管理人员显示的按钮
    @InjectView(R.id.lin_chuli)
    LinearLayout linChuli;//处理人员显示的按钮
    @InjectView(R.id.btn_order_ok)
    Button mBtnOrderOk;//处理人员确认工单
    @InjectView(R.id.btn_change)
    Button mBtnChange;//处理人员申请转派

    @InjectView(R.id.lin_jibie)
    LinearLayout lin_jibie;
    @InjectView(R.id.lin_laiyuan)
    LinearLayout lin_laiyuan;

    @InjectView(R.id.tvt_fshwijian)
    TextView mTvFshwijian;//服务时间
    @InjectView(R.id.tvt_xiangmu)
    TextView tvt_xiangmu;//报修项目

    public ImageAdapter mAdapter = null;
    List<String> mList = new ArrayList<>();

    int tag = 0;//1是公共报修，2是个人报修，3是投诉，4是建议
    public int id;

    private OrderDetil orderDetil = new OrderDetil();
    private String INTERFACE_PATH = "";//接口路径//获取详情的
    private String INTERFACE_PATH_OK = "";//接口路径//确认工单的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_detail);
        ButterKnife.inject(this);
        if (this.getIntent().getExtras() != null) {
            Bundle bundle = this.getIntent().getExtras();
            tag = bundle.getInt("tag");
            id = bundle.getInt("id");
        }
        initView();
        showBtn();
        initData();
    }

    public void initView() {
        if (MyApp.instance.isManage()) {
            linChuli.setVisibility(View.GONE);
            linGuanli.setVisibility(View.VISIBLE);
            mTvLaiyuan.setVisibility(View.VISIBLE);
        } else {
            linChuli.setVisibility(View.VISIBLE);
            linGuanli.setVisibility(View.GONE);
            lin_jibie.setVisibility(View.GONE);
            lin_laiyuan.setVisibility(View.GONE);
            mTvLaiyuan.setVisibility(View.GONE);
            mTvFshwijian.setVisibility(View.VISIBLE);
        }
        title.setText("报修详情");
        if (tag == 3) {
            title.setText("投诉详情");
            mTvFshwijian.setVisibility(View.GONE);
            tvt_xiangmu.setVisibility(View.GONE);
        } else if (tag == 4) {
            title.setText("建议详情");
            mTvFshwijian.setVisibility(View.GONE);
            tvt_xiangmu.setVisibility(View.GONE);
        }
        //这三行代码是把页面定位到最上面
        linTop.setFocusable(true);
        linTop.setFocusableInTouchMode(true);
        linTop.requestFocus();

        mBtnDistra.setOnClickListener(this);
        mBtnDeal.setOnClickListener(this);
        mBtnOrderOk.setOnClickListener(this);
        mBtnChange.setOnClickListener(this);

    }

    public void showBtn() {
        if (tag == 3)
            mBtnDeal.setVisibility(View.VISIBLE);
        else if (tag == 4) {
            mTvLeixing.setVisibility(View.GONE);
            mBtnDistra.setText("确认完成");
        }
    }

    public void initData() {
        if (tag == 1) {
            INTERFACE_PATH = RequestMapDev.GetOrderDetil;
            INTERFACE_PATH_OK = RequestMapDev.doneOrder;
        } else if (tag == 2) {
            INTERFACE_PATH = RequestMapDev.GetOrderDetil2;
            INTERFACE_PATH_OK = RequestMapDev.doneOrder2;
        } else if (tag == 3) {
            INTERFACE_PATH = RequestMapDev.GetOrderDetil3;
            INTERFACE_PATH_OK = RequestMapDev.doneOrder3;
        } else if (tag == 4) {
            INTERFACE_PATH = RequestMapDev.GetOrderDetil4;
        }
        getOrderDetil();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_distribution:
                if (!mBtnDistra.getText().toString().equals("确认完成")) {
                    Intent intent = new Intent(this, SelectPeopleActivity.class);
                    bundle.putInt("orderId", orderDetil.getId());
                    bundle.putInt("tag", tag);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(this, DoneOrderActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("tag", tag);
                    bundle.putInt("orderId", orderDetil.getId());
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.btn_order_ok:
                doneOrder();
                break;
            case R.id.btn_deal:
                Intent intent2 = new Intent(this, OrderComplete2Activity.class);
                Bundle bundle2 = new Bundle();
                bundle2.putInt("orderId", id);
                intent2.putExtras(bundle2);
                startActivity(intent2);
                finish();
                break;
            case R.id.btn_change:
                Intent intent3 = new Intent(this, Change.class);
                Bundle bundle = new Bundle();
                bundle.putInt("tag", tag);
                bundle.putInt("id", orderDetil.getAssignmentRecordId());
                bundle.putInt("orderId", orderDetil.getId());
                intent3.putExtras(bundle);
                startActivity(intent3);
                finish();
                break;
        }

    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    /**
     * 获取受理工单详情
     */
    private void getOrderDetil() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        mAjaxParam.put("orderId", id + "");
        if (MyApp.instance.isManage()){
            mAjaxParam.put("type", "1");
        }else {
            mAjaxParam.put("type", "0");
        }
        FinalHttpUtil.get(INTERFACE_PATH, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        orderDetil = gson.fromJson(data, OrderDetil.class);
                        setData(orderDetil);
                    } else {
                        ToastUtil.show(RepairDetailActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(RepairDetailActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(RepairDetailActivity.this, strMsg);
            }
        });
    }

    public void setData(OrderDetil orderDetil) {
        mTvAddress.setText(orderDetil.getAddress());
        mTvtName.setText(orderDetil.getApplicationName());
        mTvNumber.setText(orderDetil.getPhoneNumber());
        if (!TextUtils.isEmpty(orderDetil.getType())) {
            mTvLeixing.setText("报修类型:" + orderDetil.getType());
        } else {
            mTvLeixing.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(orderDetil.getProject())) {
            tvt_xiangmu.setText("报修项目:" + orderDetil.getProject());
        } else {
            tvt_xiangmu.setVisibility(View.GONE);
        }
        if (tag == 1 || tag == 2) {
            mTvNeirong.setText("报修内容:" + orderDetil.getContent());
            mTvShijian.setText("报修时间:" + orderDetil.getCreateTime());
        } else if (tag == 3) {
            mTvNeirong.setText("投诉内容:" + orderDetil.getContent());
            mTvShijian.setText("投诉时间:" + orderDetil.getCreateTime());
        } else if (tag == 4) {
            mTvNeirong.setText("建议内容:" + orderDetil.getContent());
            mTvShijian.setText("建议时间:" + orderDetil.getCreateTime());
        }
        mTvLaiyuan.setText("工单来源:" + orderDetil.getSource());
        if (!TextUtils.isEmpty(orderDetil.getLevel())) {
            mTvJibie.setText("报修级别:" + orderDetil.getLevel());
        } else {
            lin_jibie.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(orderDetil.getServiceTime())) {
            mTvFshwijian.setText("服务时间:" + orderDetil.getServiceTime());
            mTvFshwijian.setVisibility(View.VISIBLE);
        }else {
            mTvFshwijian.setVisibility(View.GONE);
        }
        if (orderDetil.getPictureUrl() != null) {
            mList.addAll(orderDetil.getPictureUrl());
            mAdapter = new ImageAdapter(this, mList);
            mGridView.setAdapter(mAdapter);
        }
    }

    /**
     * 工单确认
     */
    private void doneOrder() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        if (tag == 2) {
            mAjaxParam.put("assignmentRecordId", orderDetil.getAssignmentRecordId() + "");
        } else {
            mAjaxParam.put("id", orderDetil.getId() + "");
        }
        FinalHttpUtil.get(INTERFACE_PATH_OK, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        ToastUtil.show(RepairDetailActivity.this, "确认成功");
                        finish();
                    } else {
                        ToastUtil.show(RepairDetailActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(RepairDetailActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(RepairDetailActivity.this, strMsg);
            }
        });
    }

}
